package com.lindroy.androidplayer.ui.main.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.ConcatAdapter
import com.elvishew.xlog.XLog
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.BaseListFragment
import com.lindroy.androidplayer.databinding.FragmentHomeBinding
import com.lindroy.androidplayer.logic.network.base.http.observeResult
import com.lindroy.androidplayer.ui.adapter.ArticleAdapter
import com.lindroy.androidplayer.ui.adapter.BannerTypeAdapter
import com.lindroy.androidplayer.ui.webview.WebViewFragment
import com.lindroy.androidplayer.utils.*
import com.lindroy.androidplayer.viewmodels.HomeViewModel
import com.lindroy.androidplayer.viewmodels.SettingsViewModel

class HomeFragment : BaseListFragment<FragmentHomeBinding, HomeViewModel>() {

    private val settingsViewModel by activityViewModels<SettingsViewModel>()

    private val bannerAdapter by lazy { BannerTypeAdapter(viewLifecycleOwner) }

    private val articleAdapter by lazy { ArticleAdapter(viewLifecycleOwner) }

    private val concatAdapter by lazy {
        ConcatAdapter(
            bannerAdapter,
            articleAdapter.withFooter()
        )
    }

    override fun FragmentHomeBinding.initView(savedInstanceState: Bundle?) {
        initToolbar(R.string.home, false)
        mViewMode.setLayoutStateFlow(articleAdapter.loadStateFlow)
        this.homeVM = mViewMode
        rvArticle.adapter = concatAdapter
        rvArticle.addSimpleDivider()
    }

    override fun initData() {
        super.initData()
        getBannerData()
    }

    override fun FragmentHomeBinding.initListener() {
        mViewMode.bannerLiveData.observeResult(viewLifecycleOwner) {
            onSuccess {
                bannerAdapter.setData(mViewMode.transBannerList(it))
            }
        }

        mViewMode.bannerItemClickAction.observe(viewLifecycleOwner) {
            WebViewFragment.start(this@HomeFragment, it.id, it.url, it.title, false)
        }

        mViewMode.articleItemClickLiveData.observe(viewLifecycleOwner) {
            WebViewFragment.start(this@HomeFragment, it.id, it.link, it.title, it.collect)
        }

        mViewMode.articleListData.collectListWhenResumed(viewLifecycleOwner) {
            articleAdapter.submitData(it)
        }

        settingsViewModel.bannerCheckedFlow.collectWhenResumed(viewLifecycleOwner) {
            it.yes {
                mViewMode.getBanner()
            }.no {
                bannerAdapter.clearData()
            }
        }

        settingsViewModel.topArticleFlow.collectWhenResumed(viewLifecycleOwner) {
            articleAdapter.refresh()
        }
    }

    override fun onRefresh() {
        getBannerData()
        articleAdapter.refresh()
    }

    override fun onRetry(view: View) {
        getBannerData()
        articleAdapter.refresh()
    }

    private fun getBannerData() {
        kvIsShowBanner.yes { mViewMode.getBanner() }
    }

    companion object{
        fun newInstance() = HomeFragment()
    }
}